package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.PulseInfoQueryPageResult;
import com.sgzn.sgpay.persistence.domain.TPulseInfo;
import com.sgzn.sgpay.service.SgsPulseInfoService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 脉冲管理控制器
 * @author huangsenguang
 * @version Id: PulseInfoController.java, v 0.1 2018/5/1 10:59 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/pulseInfo")
public class PulseInfoController extends MerchantBaseController {
    @Autowired
    private SgsPulseInfoService sgsPulseInfoService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 添加脉冲管理
     * @param request
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public SgsResultResponse<String> addPulseInfo(PulseInfoAddRequest request) {
        super.setMchInfo(request);
        return this.sgsPulseInfoService.addPulseInfo(assembleAddRequest(request));
    }

    /**
     * 删除脉冲管理
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public SgsResultResponse<String> deletePulseInfo(PulseInfoDeleteRequest request) {
        super.setMchInfo(request);
        return this.sgsPulseInfoService.deletePulseInfo(assembleDeleteRequest(request));
    }

    /**
     * 查询脉冲管理的信息
     * @param request
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public SgsResultResponse<TPulseInfo> queryPulseInfoInfo(PulseInfoQueryByIdRequest request) {
        super.setMchInfo(request);
        return this.sgsPulseInfoService.queryPulseInfo(assembleQueryRequest(request));
    }

    /**
     * 分页查询脉冲管理的信息
     * @param request
     * @return
     */
    @RequestMapping("queryPage")
    @ResponseBody
    public SgsResultResponse<PulseInfoQueryPageResult> queryPagePulseInfo(PulseInfoQueryPageRequest request) {
        super.setMchInfo(request);
        return this.sgsPulseInfoService.queryPage(assembleQueryPageRequest(request));
    }

    /**
     * 更新脉冲管理信息
     * @param request
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public SgsResultResponse<String> updatePulseInfo(PulseInfoUpdateRequest request) {
        super.setMchInfo(request);
        return this.sgsPulseInfoService.updatePulseInfo(assembleUpdateRequest(request));
    }

    private SgsPulseInfoAddRequest assembleAddRequest(PulseInfoAddRequest request) {
        SgsPulseInfoAddRequest sgsPulseInfoAddRequest = new SgsPulseInfoAddRequest();
        this.assembleCommonHandle.assemble(request, sgsPulseInfoAddRequest);
        sgsPulseInfoAddRequest.setDeviceId(request.getDeviceId());
        sgsPulseInfoAddRequest.setPulseValue(request.getPulseValue());
        return sgsPulseInfoAddRequest;
    }

    private SgsPulseInfoDeleteRequest assembleDeleteRequest(PulseInfoDeleteRequest request) {
        SgsPulseInfoDeleteRequest sgsPulseInfoDeleteRequest = new SgsPulseInfoDeleteRequest();
        this.assembleCommonHandle.assemble(request, sgsPulseInfoDeleteRequest);
        sgsPulseInfoDeleteRequest.setDeviceId(request.getDeviceId());
        return sgsPulseInfoDeleteRequest;
    }

    private SgsPulseInfoQueryRequest assembleQueryRequest(PulseInfoQueryByIdRequest request) {
        SgsPulseInfoQueryRequest sgsPulseInfoQueryRequest = new SgsPulseInfoQueryRequest();
        this.assembleCommonHandle.assemble(request, sgsPulseInfoQueryRequest);
        sgsPulseInfoQueryRequest.setDeviceId(request.getDeviceId());
        return sgsPulseInfoQueryRequest;
    }

    private SgsPulseInfoUpdateRequest assembleUpdateRequest(PulseInfoUpdateRequest request) {
        SgsPulseInfoUpdateRequest sgsPulseInfoRequest = new SgsPulseInfoUpdateRequest();
        this.assembleCommonHandle.assemble(request, sgsPulseInfoRequest);
        sgsPulseInfoRequest.setDeviceId(request.getDeviceId());
        sgsPulseInfoRequest.setPulseValue(request.getPulseValue());
        return sgsPulseInfoRequest;
    }

    private SgsPulseInfoQueryPageRequest assembleQueryPageRequest(PulseInfoQueryPageRequest request) {
        SgsPulseInfoQueryPageRequest sgsPulseInfoQueryPageRequest = new SgsPulseInfoQueryPageRequest();
        this.assembleCommonHandle.assemble(request, sgsPulseInfoQueryPageRequest);
        sgsPulseInfoQueryPageRequest.setDeviceId(request.getDeviceId());
        sgsPulseInfoQueryPageRequest.setPulseValue(request.getPulseValue());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.valueOf(request.getPageNum()));
        sgsPulseInfoQueryPageRequest.setPageQueryParam(pageQueryParam);
        return sgsPulseInfoQueryPageRequest;
    }
}

